import 'package:agora_rtc_engine/rtc_engine.dart';
import 'package:agora_rtc_engine/rtc_local_view.dart' as RtcLocalView;
import 'package:agora_rtc_engine/rtc_remote_view.dart' as RtcRemoteView;
import 'package:agora_rtc_rawdata/agora_rtc_rawdata.dart';
import 'package:agora_rtc_rawdata_example/config/agora.config.dart' as config;
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  RtcEngine _engine = null;

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool isJoined = false;
  String logStr = "";

  int remoteUid;

  @override
  void initState() {
    super.initState();
    this._initEngine();
  }

  @override
  void dispose() {
    super.dispose();
    widget._engine?.destroy();
  }

  _initEngine() async {
    if (defaultTargetPlatform == TargetPlatform.android) {
      await [Permission.microphone, Permission.camera].request();
    }

    widget._engine = await RtcEngine.create(config.appId);

    widget._engine?.setEventHandler(
        RtcEngineEventHandler(joinChannelSuccess: (channel, uid, elapsed) {
      log('joinChannelSuccess $channel $uid $elapsed');
      setState(() => isJoined = true);
    }, userJoined: (uid, elapsed) {
      log('userJoined  $uid $elapsed');
      setState(() => remoteUid = uid);
    }));
    await widget._engine.enableAudio();
    AgoraRtcRawdata.init();
    await AgoraRtcRawdata.registerAudioFrameObserver(
        await widget._engine.getNativeHandle(),(data){
          log(data[0].toString()+" ,length:"+data.length.toString());
    });
    // await AgoraRtcRawdata.registerVideoFrameObserver(
    //     await widget._engine.getNativeHandle());
    // await widget._engine
    //     ?.joinChannel(config.token, config.channelId, null, config.uid);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            SizedBox(
              height: 20,
            ),

            FlatButton(
              onPressed: () async {
                //engine.setPlaybackAudioFrameParameters(samplesPerCall: 1024);

                await widget._engine.joinChannel(
                      config.token,
                    config.channelId,
                    null,
                    0);
              },
              child: Text("加入频道"),
              color: Colors.blueAccent,
            ),
            SizedBox(
              height: 10,
            ),
            FlatButton(
              onPressed: () async {
                await widget._engine.leaveChannel();
              },
              child: Text("退出频道"),
              color: Colors.redAccent,
            ),
            Expanded(
                child: SelectableText(
                  logStr,
                  style: TextStyle(fontSize: 12, color: Colors.black54),
                  maxLines: 20,
                ))
          ],
        ),
      ),
    );
  }

  void log(String str) {
    print(str);
    setState(() {
      logStr += str + "\n";
    });
  }
}
